package com.agree;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import com.google.common.base.Stopwatch;

public class MyPanel extends Panel{  
 
	private static final int IMG_WIDTH = 440;
	private static final int IMG_HIGHT = 900;
 
	private static final long serialVersionUID = 1L;

	private final Image screenImage = new BufferedImage(1080, 2340, 2);
//	private final Image screenImage = new BufferedImage(2340, 1080, 2);
      
    private final Graphics2D screenGraphic = (Graphics2D) screenImage.getGraphics();  
      
    private Image backgroundImage;  
    JLabel label;
    
    public MyPanel() {
        // 设定焦点在本窗体  
        setFocusable(true);  
        // 设定初始构造时面板大小,这里先采用图片的大小  
        setPreferredSize(new Dimension(IMG_WIDTH,IMG_HIGHT));  
  //      screenGraphic.scale(0.5, 0.5);
        label  = new JLabel();
        
        consumer();
    }  
      
    /** 
     * 载入图像 
     */  
    private void loadImage(byte[] imgs) {  
    	
    	/*
    	BufferedImage bufferedImage = null;
    	InputStream in = new ByteArrayInputStream(imgs);
    	try {
			bufferedImage = ImageIO.read(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
    	Image scaledInstance = bufferedImage.getScaledInstance(415,900, BufferedImage.SCALE_SMOOTH);
    	ImageIcon icon = new ImageIcon();
    	
    	icon.setImage(scaledInstance);
    	backgroundImage =icon.getImage(); 
    	*/
        //获得当前类对应的相对位置image文件夹下的背景图像  
    	ImageIcon icon = new ImageIcon(imgs);
    	
        //将图像实例赋给backgroundImage  
        backgroundImage = icon.getImage();  
     //   Image scaledInstance = backgroundImage.getScaledInstance(415,900, BufferedImage.SCALE_SMOOTH);
    //    Stopwatch st = Stopwatch.createStarted();
     //   icon.setImage(scaledInstance);
       
    //    backgroundImage = scaledInstance;//icon.getImage(); 
  //      System.out.println(st.stop());
   
        this.repaint();
    }  
      
//    private void drawView() {
//    	
//  //     screenGraphic.drawImage(scaledInstance, 0, 0, null);
//    	// label.imageUpdate(backgroundImage, PROPERTIES, 0,0,415,900);
//        screenGraphic.drawImage(backgroundImage, 0, 0, null);
//    }  

    public void paint(Graphics g) {
    	screenGraphic.drawImage(backgroundImage, 0, 0, null);
    	g.drawImage(backgroundImage, 0, 0, null);
    	update(getGraphics());
    }

    public void update(Graphics g) {
    	if(backgroundImage == null) {
    		backgroundImage = this.createImage(415,900);
    		Graphics gimage = backgroundImage.getGraphics();
    		paint(gimage);
    		g.drawImage(backgroundImage, 0, 0, null);
    	}
    	g.drawImage(backgroundImage, 0, 0, null);
    }
    
    public static ConcurrentLinkedDeque<byte[]> queue = new ConcurrentLinkedDeque<byte[]>();
    
    ExecutorService pool = Executors.newFixedThreadPool(5);
  
    public void consumer() {
    	
    	new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
		    		byte[] poll = queue.poll();
		    		if(poll !=null) {
		    			//loadImage(poll);
		    			pool.submit(new Task(poll));
		    		}
		    	}
			}
		}).start();
    }
    
    private class Task implements Runnable{
    	
    	byte[] poll;
    	public Task(byte[] poll) {
    		this.poll = poll;
    	}
    	@Override
		public void run() {
    		loadImage(poll);
		}
    }
    // 使用观察者 貌似会很慢
//	public void propertyChange(PropertyChangeEvent evt) {
//		
//		byte[] newValue = (byte[])evt.getNewValue();
//		loadImage(newValue);
//		  // 绘制背景  
//	    //  drawView();
//	      
//	     this.repaint();
//	}

}
